package com.demo.hc.controller.login;

import com.demo.hc.biz.account.exception.UserAccountException;
import com.demo.hc.biz.account.service.UserAccountService;
import com.demo.hc.common.constant.WebConstant;
import com.demo.hc.common.web.JsonResult;
import com.demo.hc.controller.base.BaseController;
import com.demo.hc.model.mbg.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@Description("登录控制器")
public class LoginController extends BaseController {

    @Autowired
    private UserAccountService userAccountService;

    @Description("程序启动跳转到登录页面")
    @RequestMapping(value = "index",method= RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response){
        long userId = getUserId(request);
        if (userId > 0) {
            return "redirect:/homePage";
        }else{
            return "login/login";
        }
    }

    @Description("登录")
    @RequestMapping(value = "login",method= RequestMethod.POST)
    @ResponseBody
    public JsonResult login(HttpServletRequest request, HttpServletResponse response, HttpSession session,
                            @RequestParam(value = "mobilePhone") String mobilePhone, @RequestParam(value = "password") String password){
        try {
            UserAccount userAccount = userAccountService.getUserAccount(mobilePhone, password);
            Cookie userInfoCookie = new Cookie("userAccount", userAccount.getMobilePhone() + "-" + userAccount.getName());
            userInfoCookie.setMaxAge(60 * 30);
            userInfoCookie.setPath("/");
            response.addCookie(userInfoCookie);
            session.setAttribute(WebConstant.CURRENT_LOGIN_USER_INFO, userAccount);
            session.setAttribute(WebConstant.CURRENT_LOGIN_USER_ID, String.valueOf(userAccount.getId()));
            session.setAttribute(WebConstant.CURRENT_LOGIN_USER_NAME, userAccount.getName());
        } catch (UserAccountException e) {
            logger.error("登录异常！", e);
            return JsonResult.error(e.getErrorCode(), messageSource);
        }
        return JsonResult.getResult(true);
    }

    @Description("登陆成功后跳转页面")
    @RequestMapping(value = "homePage",method= RequestMethod.GET)
    public String homePage(){
        return "homePage";
    }

    @Description("登出")
    @RequestMapping(value = "logout",method= RequestMethod.GET)
    public String logout(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute(WebConstant.CURRENT_LOGIN_USER_INFO);
        request.getSession().removeAttribute(WebConstant.CURRENT_LOGIN_USER_ID);
        request.getSession().removeAttribute(WebConstant.CURRENT_LOGIN_USER_NAME);
        return "redirect:/index";
    }

}
